package org.jeecg.modules.demo.memberenterstorerecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.basemembervip.service.IBaseMemberVipService;
import org.jeecg.modules.demo.basemembervipcoupon.service.IBaseMemberVipCouponService;
import org.jeecg.modules.demo.baseordercommoditywriteoff.service.IBaseOrderCommodityWriteoffService;
import org.jeecg.modules.demo.baseorderservicewriteoff.service.IBaseOrderServiceWriteoffService;
import org.jeecg.modules.demo.baseorderwriteoffrecord.service.IBaseOrderWriteoffRecordService;
import org.jeecg.modules.demo.memberenterstorerecord.entity.MemberEnterStoreRecord;
import org.jeecg.modules.demo.memberenterstorerecord.service.IMemberEnterStoreRecordService;
import org.jeecg.modules.demo.membertag.service.IMemberTagService;
import org.jeecg.modules.demo.msgtemplatelist.service.IMsgTemplateService;
import org.jeecg.modules.demo.tag.service.ITagService;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 会员到店记录
 */
@Api(tags = "会员到店记录")
@RestController
@RequestMapping("/memberenterstorerecord")
@Slf4j
public class MemberEnterStoreRecordController extends JeecgController<MemberEnterStoreRecord, IMemberEnterStoreRecordService> {

    @Autowired
    private IMemberEnterStoreRecordService memberEnterStoreRecordService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IBaseMemberVipService baseMemberVipService;
    @Autowired
    private IBaseMemberVipCouponService baseMemberVipCouponService;
    @Autowired
    private IBaseOrderServiceWriteoffService baseOrderServiceWriteoffService;
    @Autowired
    private IBaseOrderCommodityWriteoffService baseOrderCommodityWriteoffService;
    @Autowired
    private IBaseOrderWriteoffRecordService baseOrderWriteoffRecordService;
    @Autowired
    private IMemberTagService memberTagService;
    @Autowired
    private ITagService tagService;
    @Autowired
    private IMsgTemplateService msgTemplateListService;
    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "会员到店记录-分页列表查询")
    @ApiOperation(value = "会员到店记录-分页列表查询", notes = "会员到店记录-分页列表查询")
    @GetMapping(value = "/pageList")
    public Result<?> queryPageList(MemberEnterStoreRecord memberEnterStoreRecord,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }


        QueryWrapper<MemberEnterStoreRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        queryWrapper.eq("userinfo_status", "1");
        QueryGenerator.initQueryWrapper(queryWrapper, memberEnterStoreRecord, req.getParameterMap());
        Page<MemberEnterStoreRecord> page = new Page<>(pageNo, pageSize);
        IPage<MemberEnterStoreRecord> pageList = memberEnterStoreRecordService.page(page, queryWrapper);
        List<MemberEnterStoreRecord> records = pageList.getRecords();

        return Result.OK(pageList);
    }


    @AutoLog(value = "会员到店记录-分页列表查询")
    @ApiOperation(value = "会员到店记录-分页列表查询", notes = "会员到店记录-分页列表查询（？）")
    @GetMapping(value = "/queryList")
    public Result<?> queryList(MemberEnterStoreRecord memberEnterStoreRecord, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        QueryWrapper<MemberEnterStoreRecord> queryWrapper = QueryGenerator.initQueryWrapper(memberEnterStoreRecord, req.getParameterMap());
        queryWrapper.eq("store_id", storeId);
        List<MemberEnterStoreRecord> pageList = memberEnterStoreRecordService.list(queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 添加
     */
    @AutoLog(value = "会员到店记录-添加")
    @ApiOperation(value = "会员到店记录-添加", notes = "会员到店记录-添加")
    @PostMapping(value = "/add")
    @Transactional(rollbackFor = JeecgBootException.class)
    public Result<?> add(@RequestBody MemberEnterStoreRecord memberEnterStoreRecord, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }


        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     */
    @AutoLog(value = "会员到店记录-编辑")
    @ApiOperation(value = "会员到店记录-编辑", notes = "会员到店记录-编辑")
    @PutMapping(value = "/edit")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> edit(@RequestBody MemberEnterStoreRecord memberEnterStoreRecordVo, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }


        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "会员到店记录-通过id删除")
    @ApiOperation(value = "会员到店记录-通过id删除", notes = "会员到店记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        memberEnterStoreRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "会员到店记录-批量删除")
    @ApiOperation(value = "会员到店记录-批量删除", notes = "会员到店记录-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.error("暂不支持批量删除!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "会员到店记录-通过id查询")
    @ApiOperation(value = "会员到店记录-通过id查询", notes = "会员到店记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MemberEnterStoreRecord memberEnterStoreRecord = memberEnterStoreRecordService.getById(id);
        if (memberEnterStoreRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(memberEnterStoreRecord);
    }

}
